/*******************************************************************************
 * 版权：北京润尼尔网络科技有限公司，保留所有版权<br>
 * 版本：v1.0.0<br>
 * 文件：CommandDelete.js<br>
 * 日期：2016年9月20日<br>
 * 作者: 王志远<br>
 * 功能：删除命令<br>
 * 
 * 修改记录：<br>
 * 日期 描述 更新功能<br>
 ******************************************************************************/

window.makeNamespace(window,"Rainier.UI.V2");

/**
 * 类型名称：Rainier.UI.V2.CommandDelete<br>
 * 类型功能：删除元件命令<br>
 */
Rainier.UI.V2.CommandDelete = draw2d.command.CommandDelete.extend({
	
	NAME : "Rainier.UI.V2.CommandDelete",
	
	/**
	 * 函数名称：Rainier.UI.V2.CommandDelete.init<br>
	 * 函数功能：构造函数<br>
	 * 异常信息：无<br>
	 */
	init : function(editPart){
		this.editPart = editPart;
		this.canvas   = this.editPart.getFigure().getCanvas();
		if(this.canvas){
			this.context  = this.canvas.context;
		}
		this.parentPart = this.editPart.getParent();
		this._super(editPart.getFigure());
		
		this.connList = null;
		
		/**
		 * deleteInfo 定义：{};
		 * {
		 * 	model 	 :"",
		 * 	ports 	 :"",
		 * 	deleteConnInfoStack : [
		 *		{	
		 *			connType	:"",
		 *			connModel		: {
		 *				connection :"",
		 *				sourcePort :"",
		 *				targetPort :"",
		 *			},
		 *			sourceModel		:{
		 *				jointModel:"",
		 *				hybirdPort:"",
		 *			},
		 *			targetModel		:{
		 *				jointModel:"",
		 *				hybirdPort:"",
		 *			},
		 *  	},
		 *  	{},
		 *  	{},
		 * ]
		 */
		this.deleteInfo = null;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.CommandDelete.canExecute<br>
	 * 函数功能：判断是否可以删除<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：Boolean 是否可删除<br>
	 * 异常信息：<br>
	 */
    canExecute: function()
    {
        return this.editPart.getFigure().getCanvas()!==null;
    },


    /**
	 * 函数名称：Rainier.UI.V2.CommandDelete.execute<br>
	 * 函数功能：执行添加功能<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
    execute: function()
    {
    	if(this.editPart){
        	var model = this.editPart.getModel();
        	var parentModel = this.parentPart.getModel();
        	if(model instanceof Rainier.UI.V2.DeviceModel){
        		this.deleteInfo = parentModel.removeChildModel(model);
        	}
        	else if(model instanceof Rainier.UI.V2.ConnectionModel){
        		this.deleteInfo = parentModel.removeConnection(model);
        	}
//    		this.context.circuitparser.parse();
        };
    },
    
    /**
	 * 函数名称：Rainier.UI.V2.CommandDelete.undo<br>
	 * 函数功能：撤销命令<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
    undo: function()
    {
    	if(this.deleteInfo){
    		var _this = this;
    		var currentModel 	 = this.deleteInfo.model;
    		var connInfoStack	 = this.deleteInfo.deleteConnInfoStack.clone();
    		if(currentModel instanceof Rainier.UI.V2.DeviceModel){
    			var portList 	= this.deleteInfo.ports.clone();
    			portList.each(function(index,port){
    				currentModel.addPort(port);
    			});
    			var parentModel = currentModel.getParent();
    			parentModel.addChildModel(currentModel);
    			var uiProperty = currentModel.getProperty().uiProperty;
    			currentModel.setPosition(uiProperty.x,uiProperty.y,false);
    		}
    		
    		//翻转数组，逆序恢复数组中的内容
    		connInfoStack.reverse();
    		
    		//恢复连线：
    		connInfoStack.each(function(index,connModelInfo){
    			_this.addConnectionModel(connModelInfo);
    		})
    		var rootEditPart = this.context.getRootEditPart();
    		rootEditPart.refreshChildren();
			
//			this.context.circuitparser.parse();
    	}
    },

    /**
	 * 函数名称：draw2d.command.CommandDelete.redo<br>
	 * 函数功能：重做命令<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
    redo: function()
    {
    	this.execute();
    },
    
    /**
	 * 函数名称：draw2d.command.CommandDelete.addConnectionModel<br>
	 * 函数功能：恢复连线信息的添加<br>
	 * 参数输入：connModelInfo:封装好的connModelInfo信息<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
    addConnectionModel : function(connModelInfo){
    	var connType	= connModelInfo.connType;
		var connObj 	= connModelInfo.connModel;
		var sourceModel = connModelInfo.sourceModel;
		var targetModel = connModelInfo.targetModel;
		
		//第一步：把当前连线添加进来
		var conn 	   = connObj.connection;

		//第二步：恢复连线两端的jointModel
		if(sourceModel){
			var jointModel 		 = sourceModel.jointModel;
			var hybridPort		 = sourceModel.hybirdPort;
			
			var jointModelId	 = jointModel.getUuid();
			
			var jointModelParent  = jointModel.getParent();
			var connJointModelIds = [];
			var connJointModels   = jointModelParent.getChildren();
			connJointModels.forEach(function(connJointModel,index,connJointModels){
				connJointModelIds.push(connJointModel.getUuid());
			})
			//不存在当前model
			if(connJointModelIds.indexOf(jointModelId) === -1){
				jointModel.addPort(hybridPort);
				jointModelParent.addChildModel(jointModel);    					
			}
		}
		
		if(targetModel){
			var jointModel 		 = targetModel.jointModel;
			var hybridPort		 = targetModel.hybirdPort;
			
			var jointModelId	 = jointModel.getUuid();
			
			var jointModelParent  = jointModel.getParent();
			var connJointModelIds = [];
			var connJointModels   = jointModelParent.getChildren();
			connJointModels.forEach(function(connJointModel,index,connJointModels){
				connJointModelIds.push(connJointModel.getUuid());
			})
			//不存在当前model
			if(connJointModelIds.indexOf(jointModelId) === -1){
				jointModel.addPort(hybridPort);
				jointModelParent.addChildModel(jointModel);    					
			}
		}
		
		//第三步：恢复连线和sourcePort和targetPort的关联关系。
		var sourcePort = connObj.sourcePort;
		var targetPort = connObj.targetPort;
		conn.attachSourcePort(sourcePort);
		conn.attachTargetPort(targetPort);
		sourcePort.attachOutgoing(conn);
		targetPort.attachIncoming(conn);
			
		var connParent = conn.getParent();
		connParent.addConnection(conn);
		
//		this.context.rootModel.setConnectionPortValue(sourcePort,targetPort);
    }
});

